﻿Imports System
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.Activities
    <ActivityDesignerTheme(GetType(SetStateDesignerTheme)), ComVisible(False)> _
    Friend NotInheritable Class SetStateDesigner
        Inherits ActivityDesigner
        ' Methods
        Public Overrides Function CanBeParentedTo(ByVal parentActivityDesigner As CompositeActivityDesigner) As Boolean
            If (parentActivityDesigner Is Nothing) Then
                Throw New ArgumentNullException("parentActivityDesigner")
            End If
            Dim parentActivity As CompositeActivity = [TryCast](parentActivityDesigner.Activity,CompositeActivity)
            If (parentActivity Is Nothing) Then
                Return False
            End If
            If Not SetStateDesigner.ValidateParent(parentActivity) Then
                Return False
            End If
            Return MyBase.CanBeParentedTo(parentActivityDesigner)
        End Function

        Protected Overrides Sub OnActivityChanged(ByVal e As ActivityChangedEventArgs)
            MyBase.OnActivityChanged(e)
            If (Me.previousTargetState <> Me.TargetState) Then
                MyBase.PerformLayout
            End If
        End Sub

        Protected Overrides Function OnLayoutSize(ByVal e As ActivityDesignerLayoutEventArgs) As Size
            Dim size As Size = MyBase.OnLayoutSize(e)
            Dim targetState As String = Me.TargetState
            If String.IsNullOrEmpty(targetState) Then
                targetState = "M"
            End If
            Dim font As Font = e.DesignerTheme.Font
            Me.targetStateSize = StateMachineDesignerPaint.MeasureString(e.Graphics, font, targetState, StringAlignment.Near, Size.Empty)
            size.Height = (size.Height + Me.targetStateSize.Height)
            Return size
        End Function

        Protected Overrides Sub OnPaint(ByVal e As ActivityDesignerPaintEventArgs)
            MyBase.OnPaint(e)
            Dim targetState As String = Me.TargetState
            ActivityDesignerPaint.DrawText(e.Graphics, e.DesignerTheme.Font, targetState, Me.TargetStateRectangle, StringAlignment.Center, e.AmbientTheme.TextQuality, e.DesignerTheme.ForegroundBrush)
        End Sub

        Private Shared Function ValidateParent(ByVal parentActivity As CompositeActivity) As Boolean
            If (parentActivity Is Nothing) Then
                Return False
            End If
            Return (SetStateValidator.IsValidContainer(parentActivity) OrElse SetStateDesigner.ValidateParent(parentActivity.Parent))
        End Function


        ' Properties
        Protected Overrides ReadOnly Property ImageRectangle As Rectangle
            Get
                Dim margin As Size = WorkflowTheme.CurrentTheme.AmbientTheme.Margin
                Dim imageRectangle As Rectangle = MyBase.ImageRectangle
                imageRectangle.Offset(0, ((-Me.targetStateSize.Height - margin.Height) / 2))
                Return imageRectangle
            End Get
        End Property

        Private ReadOnly Property TargetState As String
            Get
                Dim activity As SetStateActivity = [TryCast](MyBase.Activity,SetStateActivity)
                If (activity Is Nothing) Then
                    Return String.Empty
                End If
                Dim targetStateName As String = activity.TargetStateName
                If (targetStateName Is Nothing) Then
                    Return String.Empty
                End If
                Return targetStateName
            End Get
        End Property

        Friend ReadOnly Property TargetStateRectangle As Rectangle
            Get
                Dim margin As Size = WorkflowTheme.CurrentTheme.AmbientTheme.Margin
                Dim bounds As Rectangle = MyBase.Bounds
                Dim textRectangle As Rectangle = Me.TextRectangle
                Dim location As New Point((bounds.Left + margin.Width), (textRectangle.Bottom + (margin.Height / 2)))
                Return New Rectangle(location, New Size((bounds.Width - (margin.Width * 2)), Me.targetStateSize.Height))
            End Get
        End Property

        Protected Overrides ReadOnly Property TextRectangle As Rectangle
            Get
                Dim margin As Size = WorkflowTheme.CurrentTheme.AmbientTheme.Margin
                Dim textRectangle As Rectangle = MyBase.TextRectangle
                textRectangle.Offset(0, ((-Me.targetStateSize.Height - margin.Height) / 2))
                Return textRectangle
            End Get
        End Property


        ' Fields
        Private previousTargetState As String = String.Empty
        Private targetStateSize As Size = Size.Empty
    End Class
End Namespace

